package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.common.baseContext;
import xyz.weizhi.rjwm.entity.AddressBook;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.AddressBookService;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.util.List;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/9/29 0029 11:12
 */
@Api(tags = "地址簿相关")
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookContorller {
    @Autowired
    private AddressBookService addressBookService;
    @ApiOperation(value = "获取地址簿列表")
    @GetMapping("/list")
    public R<List<AddressBook>> getAddressBookList(HttpServletRequest request){
        Long userID = (Long)request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userID);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    @PostMapping
    public R addAddressBook(@RequestBody AddressBook addressBook,HttpServletRequest request){
        log.info("addressBook:{}",addressBook);
        Long userID = (Long)request.getSession().getAttribute("user");
        addressBook.setUserId(userID);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBookById(@PathVariable long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getIsDefault,'1');
        if (addressBookService.getOne(luw)!=null){
            luw.set(AddressBook::getIsDefault,0);
            addressBookService.update(null,luw);
        }
        luw.or().eq(AddressBook::getId,addressBook.getId()).set(AddressBook::getIsDefault,1);
        addressBookService.update(null,luw);
        return R.success("修改成功");
    }
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}",addressBook);
        addressBookService.updateById(addressBook);
        return R.success("编辑成功");
    }
    @DeleteMapping
    public R<String> deleteAddressBookById(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }
    @GetMapping("/default")
    public R<AddressBook> getDefualtAddressBook(){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook DefualtAddressBook = addressBookService.getOne(lqw);
        return R.success(DefualtAddressBook);
    }
    @GetMapping("/lastUpdate")
    public R addressLastUpdate(){

        return null;
    }
}
